package org.example.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

/**
 * 工具类 DBUtil 获取数据库 Connection
 */

public class DBUtil {
//    static final String database = "src/main/resources/org/example/db/data.db";
//    static final String database = "org/example/db/data.db";
    static final String database = "data.db";

    // 判断是否有 org.sqlite.JDBC 类（判断是否有 sqlite-jdbc-xxx.jar）
    static {
        try {
            Class.forName("org.sqlite.JDBC");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }


    /**
     * 连接数据库
     * @return Connection
     * @throws SQLException 连接 SQLite 数据库失败
     */
    public static Connection getConnection() throws SQLException {
//        String url = String.format("jdbc:sqlite:%s", database);
        String url = String.format("jdbc:sqlite:%s", database);
        return DriverManager.getConnection(url);
    }
}
